package com.servlets;


import com.alibaba.fastjson.JSON;
import com.domain.R;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class GetAllStudentsServlet extends HttpServlet {
    int pageNo = 0;
    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        try {
            pageNo = Integer.parseInt(req.getParameter("pn"));
        } catch (Exception e) {
            pageNo = 0;
        }

        List allStudents = studentService.getAllStudents();

        R r = new R();
        r.setCode(0);
        r.setData(allStudents);

        String resJSON = JSON.toJSONString(r);     // 转换为json
        resp.getWriter().print(resJSON); // 输出
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {


    }

}
